package com.neusoft.ecommerce.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.neusoft.ecommerce.dbhelper.LayuiPager;
import com.neusoft.ecommerce.entity.AfterSaleService;
import com.neusoft.ecommerce.entity.Finance;
import com.neusoft.ecommerce.entity.Goods;
import com.neusoft.ecommerce.service.FinanceService;
import com.neusoft.ecommerce.service.GoodsService;

/**
 * 
 * @author YinKuenTam
 *
 */
@RestController
public class FinanceController {
	private final FinanceService financeService;
	private final GoodsService goodsService;

	public FinanceController(FinanceService financeService, GoodsService goodsService) {
		this.financeService = financeService;
		this.goodsService = goodsService;
	}

	@GetMapping("/finance_page")
	public ModelAndView providerPage() {
		return new ModelAndView("finance/finance-list");
	}

	@RequestMapping("/finance/list")
	@ResponseBody
	public LayuiPager<Finance> list(@RequestParam(value = "page", defaultValue = "1") String page,
			@RequestParam(value = "limit", defaultValue = "10") String limit) {
		System.out.println(page + "" + limit);
		LayuiPager<Finance> pager = new LayuiPager<>(Integer.parseInt(page), Integer.parseInt(limit));
		int count = financeService.findTotal(pager);
		List<Finance> list = financeService.findList(pager);
		for (Finance finance : list) {
			System.out.println(finance);
		}
		pager.setData(list);
		pager.setCount(count);
		pager.setCode(0);
		pager.setMsg("");
		return pager;
	}
	@GetMapping("/addFinance")
    public ModelAndView addFinance() {
        return new ModelAndView("finance/finance-add");
    }
	
	@RequestMapping("/finance/goodsList")
    public List<Goods> findGoodsList() {
	    return goodsService.findAll();
    }
	@PostMapping("/finance/add")
    public Map<String, Object> add(Finance finance) {
        System.out.println(finance);
        Map<String, Object> map = new HashMap<>(16);
        if (financeService.add(finance)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }
	@GetMapping("/finance/edit_page")
    public ModelAndView editPage(Finance finance, Model model) {
        System.out.println(finance);
        LayuiPager<Finance> pager = new LayuiPager<>(1,1);
        pager.setCondition(finance);
        Finance message = financeService.findList(pager).get(0);
        System.out.println(message);
        ModelAndView view = new ModelAndView("finance/finance-edit");
        model.addAttribute("finance", message);
        return view;
    }
	
	
	@PostMapping("/finance/edit")
    public Map<String, Object> edit(Finance finance) {
        System.out.println(finance);
        Map<String, Object> map = new HashMap<>(16);
        if (financeService.edit(finance)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }
	@PostMapping("/finance/delete")
    public Map<String, Object> delete(Finance finance) {
        System.out.println(finance);
        Map<String, Object> map = new HashMap<>(16);
        if (financeService.delete(finance)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }
	
	
	

}
